package sandau.self_improvement_wall.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sandau.self_improvement_wall.dto.LoginInfo;
import sandau.self_improvement_wall.dto.TokenInfo;
import sandau.self_improvement_wall.service.TokenService;
import sandau.self_improvement_wall.vo.BasicMsg;

@RestController
@RequestMapping("/api")
public class LoginController {
    @Resource
    private TokenService tokenService;
    @PostMapping("/login")
    public BasicMsg submit(@RequestBody LoginInfo loginInfo, HttpServletResponse response){
        BasicMsg msg = new BasicMsg(0,"");
        String account = loginInfo.getAccount();
        String password = loginInfo.getPassword();
        //完整验证
        TokenInfo tokenInfo =tokenService.generateToken("1", account);
        Cookie accesstokenCookie = new Cookie("accessToken", tokenInfo.getAccessToken());
        accesstokenCookie.setMaxAge(7*24*60*60);
        accesstokenCookie.setHttpOnly(true);
        response.addCookie(accesstokenCookie);
        return msg;
    }
}
